home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.std.c
- Subject: Re: Help, best way to compare doubles
- Date: 14 Jan 1996 15:25:42 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4dbos6$o7q@umbc9.umbc.edu>
- References: <4d1k09$aqq@mercury.IntNet.net>
- NNTP-Posting-Host: f-umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Jeff Tomich <jtomich@IntNet.net> wrote:
- |> Having a hard time to figure out a function on how to compare doubles.
- |> Any ideas?
-
- I'd go along with what the c.l.c. FAQ has to say about this one:
-
- 14.5: What's a good way to check for "close enough" floating-point
- equality?
-
- A: Since the absolute accuracy of floating point values varies, by
- definition, with their magnitude, the best way of comparing two
- floating point values is to use an accuracy threshold which is
- relative to the magnitude of the numbers being compared. Rather
- than
-
- double a, b;
- ...
- if(a == b) /* WRONG */
-
- use something like
-
- #include <math.h>
-
- if(fabs(a - b) <= epsilon * a)
-
- for some suitably-chosen epsilon.
-
- References: Knuth Sec. 4.2.2 pp. 217-8.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-